package cn.yunyichina.provider.pay.wechat.service.impl;

import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.ResponseObject;
import cn.yunyichina.provider.iface.entity.dto.pay.*;
import cn.yunyichina.provider.iface.entity.dto.wechat.WechatWebPayRequestDTO;
import cn.yunyichina.provider.iface.entity.dto.wechat.WechatWebPayResponseDTO;
import cn.yunyichina.provider.iface.entity.pay.WechatMedicalBillDownloadRequest;
import cn.yunyichina.provider.iface.entity.pay.WechatMedicalBillDownloadResponse;
import cn.yunyichina.provider.pay.wechat.common.XMLUtil;
import cn.yunyichina.provider.pay.wechat.config.Wechat;
import cn.yunyichina.provider.pay.wechat.constant.ResponseEnum;
import cn.yunyichina.provider.pay.wechat.entity.*;
import cn.yunyichina.provider.pay.wechat.entity._do.*;
import cn.yunyichina.provider.pay.wechat.entity.common.WechatCommonResponse;
import cn.yunyichina.provider.pay.wechat.service.BasePayService;
import cn.yunyichina.provider.pay.wechat.service.WechatPayService;
import cn.yunyichina.utils.commons.HttpUtil;
import cn.yunyichina.utils.commons.RandomUtil;
import cn.yunyichina.utils.convert.JsonUtils;
import cn.yunyichina.utils.dozer.Dozer;
import cn.yunyichina.utils.json.Jackson;
import cn.yunyichina.utils.security.SignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 微信支付
 * Created by Lullaby on 2016/9/19.
 */
@Service
public class WechatPayServiceImpl extends BasePayService implements WechatPayService {

    private static final Logger logger = LoggerFactory.getLogger(WechatPayServiceImpl.class);

    /**
     * 微信公众号支付统一下单
     *
     * @param json
     * @return
     */
    public ResponseObject jsapiWechatPay(String json) {
        logger.info("WechatPayServiceImpl.jsapiWechatPay.json -> {}", json);

        ResponseObject response = new ResponseObject();

        try {
            WechatJsapiPayRequest jsapiPayRequest = JsonUtils.parseObject(json, WechatJsapiPayRequest.class);

            String apiSecret = jsapiPayRequest.getApiSecret();
            jsapiPayRequest.setApiSecret(null);
            jsapiPayRequest.setSign(SignUtil.wechatPaySign(JsonUtils.parseObject(jsapiPayRequest, Map.class), apiSecret));

            String xml = XMLUtil.buildMapXML(JsonUtils.parseObject(jsapiPayRequest, Map.class));
            logger.info("WechatPayServiceImpl.jsapiWechatPay.xml -> {}", xml);

            String result = HttpUtil.post(xml, this.getConfig(Wechat.WECHAT_PAY_PAY_UNIFIED));
            logger.info("WechatPayServiceImpl.jsapiWechatPay.result -> {}", result);

            Map<String, String> resultMap = XMLUtil.xmlToMap(result);
            logger.info("WechatPayServiceImpl.jsapiWechatPay.resultMap -> {}", JsonUtils.toJsonString(resultMap));

            WechatJsapiPayResponse jsapiPayResponse = JsonUtils.parseObject(resultMap, WechatJsapiPayResponse.class);
            logger.info("WechatPayServiceImpl.jsapiWechatPay.jsapiPayResponse -> {}", JsonUtils.toJsonString(jsapiPayResponse));

            if (jsapiPayResponse.isReturnCodeSuccess()) {
                if (SignUtil.validateWechatPaySign(resultMap, apiSecret)) {
                    if (jsapiPayResponse.isResultCodeSuccess()) {

                        response.setResultObject(buildWechatJsapiPayResponse(jsapiPayResponse, apiSecret));

                    } else {
                        response.setResultCodeAndMessageAndResult(
                                ResponseEnum.WECHAT_PAY_TRADE_FAIL.getCode(),
                                ResponseEnum.WECHAT_PAY_TRADE_FAIL.getMessage(),
                                getWechatErrCodeAndDes(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                        );
                    }
                } else {
                    response.setResultCodeAndMessage(
                            ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                            ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                    );
                }
            } else {
                response.setResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        getWechatReturnCodeAndMsg(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                );
            }
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.jsapiWechatPay.exception", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }

        return response;
    }

    /**
     * 微信APP支付统一下单
     *
     * @param json
     * @return
     */
    public ResponseObject appWechatPay(String json) {
        logger.info("WechatPayServiceImpl.appWechatPay.json -> {}", json);

        ResponseObject response = new ResponseObject();

        try {
            WechatAppPayRequest appPayRequest = JsonUtils.parseObject(json, WechatAppPayRequest.class);

            String apiSecret = appPayRequest.getApiSecret();
            appPayRequest.setApiSecret(null);
            appPayRequest.setSign(SignUtil.wechatPaySign(JsonUtils.parseObject(appPayRequest, Map.class), apiSecret));

            String xml = XMLUtil.buildMapXML(JsonUtils.parseObject(appPayRequest, Map.class));
            logger.info("WechatPayServiceImpl.appWechatPay.xml -> {}", xml);

            String result = HttpUtil.post(xml, this.getConfig(Wechat.WECHAT_PAY_PAY_UNIFIED));
            logger.info("WechatPayServiceImpl.appWechatPay.result -> {}", result);

            Map<String, String> resultMap = XMLUtil.xmlToMap(result);
            logger.info("WechatPayServiceImpl.appWechatPay.resultMap -> {}", JsonUtils.toJsonString(resultMap));

            WechatAppPayResponse appPayResponse = JsonUtils.parseObject(resultMap, WechatAppPayResponse.class);
            logger.info("WechatPayServiceImpl.appWechatPay.appPayResponse -> {}", JsonUtils.toJsonString(appPayResponse));

            if (appPayResponse.isReturnCodeSuccess()) {
                if (SignUtil.validateWechatPaySign(resultMap, apiSecret)) {
                    if (appPayResponse.isResultCodeSuccess()) {

                        response.setResultObject(buildWechatAppPayResponse(appPayResponse, apiSecret));

                    } else {
                        response.setResultCodeAndMessageAndResult(
                                ResponseEnum.WECHAT_PAY_TRADE_FAIL.getCode(),
                                ResponseEnum.WECHAT_PAY_TRADE_FAIL.getMessage(),
                                getWechatErrCodeAndDes(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                        );
                    }
                } else {
                    response.setResultCodeAndMessage(
                            ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                            ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                    );
                }
            } else {
                response.setResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        getWechatReturnCodeAndMsg(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                );
            }
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.appWechatPay.exception", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }

        return response;
    }

    /**
     * 微信扫码付统一下单
     *
     * @param json
     * @return
     */
    public ResponseObject nativeWechatPay(String json) {
        logger.info("WechatPayServiceImpl.nativeWechatPay.json -> {}", json);

        ResponseObject response = new ResponseObject();

        try {
            WechatNativePayRequest nativePayRequest = JsonUtils.parseObject(json, WechatNativePayRequest.class);

            String apiSecret = nativePayRequest.getApiSecret();
            nativePayRequest.setApiSecret(null);
            nativePayRequest.setSign(SignUtil.wechatPaySign(JsonUtils.parseObject(nativePayRequest, Map.class), apiSecret));

            String xml = XMLUtil.buildMapXML(JsonUtils.parseObject(nativePayRequest, Map.class));
            logger.info("WechatPayServiceImpl.nativeWechatPay.xml -> {}", xml);

            String result = HttpUtil.post(xml, this.getConfig(Wechat.WECHAT_PAY_PAY_UNIFIED));
            logger.info("WechatPayServiceImpl.nativeWechatPay.result -> {}", result);

            Map<String, String> resultMap = XMLUtil.xmlToMap(result);
            logger.info("WechatPayServiceImpl.nativeWechatPay.resultMap -> {}", JsonUtils.toJsonString(resultMap));

            WechatNativePayResponse nativePayResponse = JsonUtils.parseObject(resultMap, WechatNativePayResponse.class);
            logger.info("WechatPayServiceImpl.nativeWechatPay.nativePayResponse -> {}", JsonUtils.toJsonString(nativePayResponse));

            if (nativePayResponse.isReturnCodeSuccess()) {
                if (SignUtil.validateWechatPaySign(resultMap, apiSecret)) {
                    if (nativePayResponse.isResultCodeSuccess()) {

                        response.setResultObject(buildWechatNativePayResponse(nativePayResponse));

                    } else {
                        response.setResultCodeAndMessageAndResult(
                                ResponseEnum.WECHAT_PAY_TRADE_FAIL.getCode(),
                                ResponseEnum.WECHAT_PAY_TRADE_FAIL.getMessage(),
                                getWechatErrCodeAndDes(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                        );
                    }
                } else {
                    response.setResultCodeAndMessage(
                            ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                            ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                    );
                }
            } else {
                response.setResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        getWechatReturnCodeAndMsg(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                );
            }
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.nativeWechatPay.exception", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }

        return response;
    }

    /**
     * 微信刷卡支付
     *
     * @param requestDTO
     * @return
     */
    public ResCommon<WechatMicroPayResponseDTO> microWechatPay(WechatMicroPayRequestDTO requestDTO) {
        try {
            String apiSecret = requestDTO.getApiSecret();

            WechatMicroPayRequestDO requestDO = Dozer.convert(requestDTO, WechatMicroPayRequestDO.class);
            requestDO.setSign(SignUtil.wechatPaySign(JsonUtils.parseObject(requestDO, Map.class), apiSecret));

            String request = XMLUtil.buildMapXML(JsonUtils.parseObject(requestDO, Map.class));
            logger.info("WechatPayServiceImpl.microWechatPay.xml -> {}", request);

            String result = HttpUtil.post(request, this.getConfig(Wechat.WECHAT_PAY_MICROPAY));
            logger.info("WechatPayServiceImpl.microWechatPay.result -> {}", result);

            Map<String, String> map = XMLUtil.xmlToMap(result);
            WechatMicroPayResponseDO responseDO = JsonUtils.parseObject(map, WechatMicroPayResponseDO.class);

            if (!responseDO.isReturnCodeSuccess()) {
                WechatMicroPayResponseDTO responseDTO = new WechatMicroPayResponseDTO();
                responseDTO.setReturnCode(responseDO.getReturnCode());
                responseDTO.setReturnMsg(responseDO.getReturnMsg());
                return new ResCommon<WechatMicroPayResponseDTO>().buildResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        responseDTO
                );
            }

            if (!SignUtil.validateWechatPaySign(map, apiSecret))
                return new ResCommon<WechatMicroPayResponseDTO>().buildResultCodeAndMessage(
                        ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                        ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                );

            return new ResCommon<WechatMicroPayResponseDTO>().buildResult(Dozer.convert(responseDO, WechatMicroPayResponseDTO.class));
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.microWechatPay.Exception", e);
            return new ResCommon<WechatMicroPayResponseDTO>().buildResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }
    }

    /**
     * 查询微信订单
     *
     * @param requestDTO
     * @return
     */
    public ResCommon<WechatOrderQueryResponseDTO> queryWechatOrder(WechatOrderQueryRequestDTO requestDTO) {
        logger.info("WechatPayServiceImpl.queryWechatOrder.requestDTO -> {}", JsonUtils.toJsonString(requestDTO));

        try {
            String apiSecret = requestDTO.getApiSecret();

            WechatOrderQueryRequestDO requestDO = Dozer.convert(requestDTO, WechatOrderQueryRequestDO.class);
            requestDO.setSign(SignUtil.wechatPaySign(JsonUtils.parseObject(requestDO, Map.class), apiSecret));
            logger.info("WechatPayServiceImpl.queryWechatOrder.requestDO -> {}", JsonUtils.toJsonString(requestDO));

            String request = XMLUtil.buildMapXML(JsonUtils.parseObject(requestDO, Map.class));
            logger.info("WechatPayServiceImpl.queryWechatOrder.xml -> {}", request);

            String response = HttpUtil.post(request, this.getConfig(Wechat.WECHAT_ORDER_QUERY));
            logger.info("WechatPayServiceImpl.queryWechatOrder.response -> {}", response);

            Map<String, String> result = XMLUtil.xmlToMap(response);
            WechatOrderQueryResponseDO responseDO = JsonUtils.parseObject(result, WechatOrderQueryResponseDO.class);
            logger.info("WechatPayServiceImpl.queryWechatOrder.responseDO -> {}", JsonUtils.toJsonString(responseDO));

            if (!responseDO.isReturnCodeSuccess()) {
                WechatOrderQueryResponseDTO responseDTO = new WechatOrderQueryResponseDTO();
                responseDTO.setReturnCode(responseDO.getReturnCode());
                responseDTO.setReturnMsg(responseDO.getReturnMsg());
                return new ResCommon<WechatOrderQueryResponseDTO>().buildResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        responseDTO
                );
            }

            if (!SignUtil.validateWechatPaySign(result, apiSecret))
                return new ResCommon<WechatOrderQueryResponseDTO>().buildResultCodeAndMessage(
                        ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                        ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                );

            if (!responseDO.isResultCodeSuccess()) {
                WechatOrderQueryResponseDTO responseDTO = new WechatOrderQueryResponseDTO();
                responseDTO.setErrCode(responseDO.getErrCode());
                responseDTO.setErrCodeDes(responseDO.getErrCodeDes());
                return new ResCommon<WechatOrderQueryResponseDTO>().buildResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_TRADE_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_TRADE_FAIL.getMessage(),
                        responseDTO
                );
            }

            WechatOrderQueryResponseDTO responseDTO = Dozer.convert(responseDO, WechatOrderQueryResponseDTO.class);
            return new ResCommon<WechatOrderQueryResponseDTO>().buildResult(responseDTO);
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.queryWechatOrder.Exception", e);
            return new ResCommon<WechatOrderQueryResponseDTO>().buildResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }
    }

    /**
     * 撤销微信订单
     *
     * @param requestDTO
     * @return
     */
    public ResCommon<WechatOrderReverseResponseDTO> reverseWechatOrder(WechatOrderReverseRequestDTO requestDTO) {
        logger.info("WechatPayServiceImpl.reverseWechatOrder.requestDTO -> {}", JsonUtils.toJsonString(requestDTO));

        try {
            String apiSecret = requestDTO.getApiSecret();

            WechatOrderReverseRequestDO requestDO = Dozer.convert(requestDTO, WechatOrderReverseRequestDO.class);
            requestDO.setSign(SignUtil.wechatPaySign(JsonUtils.parseObject(requestDO, Map.class), apiSecret));
            logger.info("WechatPayServiceImpl.reverseWechatOrder.requestDO -> {}", JsonUtils.toJsonString(requestDO));

            String request = XMLUtil.buildMapXML(JsonUtils.parseObject(requestDO, Map.class));
            logger.info("WechatPayServiceImpl.reverseWechatOrder.xml -> {}", request);

            String mchId = requestDO.getMchId();
            String certPath = "/cert/" + mchId + ".p12";
            String response = HttpUtil.doWechatSslPost(this.getConfig(Wechat.WECHAT_ORDER_REVERSE), request, mchId, certPath);
            logger.info("WechatPayServiceImpl.reverseWechatOrder.response -> {}", response);

            Map<String, String> result = XMLUtil.xmlToMap(response);
            WechatOrderReverseResponseDO responseDO = JsonUtils.parseObject(result, WechatOrderReverseResponseDO.class);
            logger.info("WechatPayServiceImpl.reverseWechatOrder.responseDO -> {}", JsonUtils.toJsonString(responseDO));

            if (!responseDO.isReturnCodeSuccess()) {
                WechatOrderReverseResponseDTO responseDTO = new WechatOrderReverseResponseDTO();
                responseDTO.setReturnCode(responseDO.getReturnCode());
                responseDTO.setReturnMsg(responseDO.getReturnMsg());
                return new ResCommon<WechatOrderReverseResponseDTO>().buildResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        responseDTO
                );
            }

            if (!SignUtil.validateWechatPaySign(result, apiSecret))
                return new ResCommon<WechatOrderReverseResponseDTO>().buildResultCodeAndMessage(
                        ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                        ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                );

            WechatOrderReverseResponseDTO responseDTO = Dozer.convert(responseDO, WechatOrderReverseResponseDTO.class);
            return new ResCommon<WechatOrderReverseResponseDTO>().buildResult(responseDTO);
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.reverseWechatOrder.Exception", e);
            return new ResCommon<WechatOrderReverseResponseDTO>().buildResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }
    }

    /**
     * 微信医疗支付统一下单
     *
     * @param json
     * @return
     */
    public ResponseObject medicalWechatPay(String json) {
        logger.info("WechatPayServiceImpl.medicalWechatPay.json -> {}", json);

        ResponseObject response = new ResponseObject();

        try {
            Map<String, String> requestMap = JsonUtils.parseObject(json, Map.class);
            logger.info("WechatPayServiceImpl.medicalWechatPay.requestMap -> {}", JsonUtils.toJsonString(requestMap));

            String accessToken = requestMap.get("access_token");
            String apiSecret = requestMap.get("api_secret");

            requestMap.remove("access_token");
            requestMap.remove("api_secret");

            requestMap.put("sign", SignUtil.wechatPaySign(requestMap, apiSecret));

            String xml = XMLUtil.buildMapXML(requestMap);
            logger.info("WechatPayServiceImpl.medicalWechatPay.xml -> {}", xml);

            String requestUrl = String.format(this.getConfig(Wechat.WECHAT_MEDICAL_PAY), accessToken);
            logger.info("WechatPayServiceImpl.medicalWechatPay.requestUrl -> {}", requestUrl);

            String result = HttpUtil.post(xml, requestUrl);
            logger.info("WechatPayServiceImpl.medicalWechatPay.result -> {}", result);

            Map<String, String> resultMap = XMLUtil.xmlToMap(result);
            logger.info("WechatPayServiceImpl.medicalWechatPay.resultMap -> {}", JsonUtils.toJsonString(resultMap));

            WechatMedicalPayResponse medicalPayResponse = JsonUtils.parseObject(resultMap, WechatMedicalPayResponse.class);
            logger.info("WechatPayServiceImpl.medicalWechatPay.medicalPayResponse -> {}", JsonUtils.toJsonString(medicalPayResponse));

            if (medicalPayResponse.isReturnCodeSuccess() && medicalPayResponse.isResultCodeSuccess()) {
                if (SignUtil.validateWechatPaySign(resultMap, apiSecret)) {
                    response.setResultObject(buildWechatMedicalPayResponse(medicalPayResponse));
                } else {
                    response.setResultCodeAndMessage(
                            ResponseEnum.WECHAT_INVALID_SIGN.getCode(),
                            ResponseEnum.WECHAT_INVALID_SIGN.getMessage()
                    );
                }
            } else {
                response.setResultCodeAndMessageAndResult(
                        ResponseEnum.WECHAT_PAY_FAIL.getCode(),
                        ResponseEnum.WECHAT_PAY_FAIL.getMessage(),
                        getWechatErrCodeAndDes(JsonUtils.parseObject(resultMap, WechatCommonResponse.class))
                );
            }
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.medicalWechatPay.exception", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }

        return response;
    }

    /**
     * 下载微信医疗支付对账单
     *
     * @param request
     * @return
     */
    public ResCommon<WechatMedicalBillDownloadResponse> downloadWechatMedicalBill(WechatMedicalBillDownloadRequest request) {
        ResCommon<WechatMedicalBillDownloadResponse> response = new ResCommon<>();

        try {
            Map<String, String> params = JsonUtils.parseObject(request, Map.class);

            String accessToken = params.get("access_token");
            String apiSecret = params.get("api_secret");

            params.remove("access_token");
            params.remove("api_secret");

            params.put("nonce_str", RandomUtil.getRandomALLChar(32));
            params.put("sign", SignUtil.wechatPaySign(params, apiSecret));

            String xml = XMLUtil.buildMapXML(params);
            logger.info("WechatPayServiceImpl.downloadWechatMedicalBill.xml -> {}", xml);

            String requestUrl = String.format(this.getConfig(Wechat.WECHAT_MEDICAL_BILL), accessToken);
            logger.info("WechatPayServiceImpl.medicalWechatPay.requestUrl -> {}", requestUrl);

            String result = HttpUtil.post(xml, requestUrl);
            logger.info("WechatPayServiceImpl.downloadWechatMedicalBill.result -> {}", result);

            response.setResult(new WechatMedicalBillDownloadResponse().setResult(result));
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.downloadWechatMedicalBill.exception", e);
            response.setResultCodeAndMessage(
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(),
                    ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage()
            );
        }

        return response;
    }

    /**
     * 微信H5支付
     *
     * @param request
     * @return
     */
    @Override
    public ResCommon<WechatWebPayResponseDTO> webPay(WechatWebPayRequestDTO request) {
        ResCommon<WechatWebPayResponseDTO> response = new ResCommon<>();
        try {
            Map<String, String> params = Jackson.fromObject(request, Map.class);

            String requestXml = XMLUtil.buildMapXML(params);
            logger.info("WechatPayServiceImpl.webPay.requestXml -> {}", requestXml);

            String requestUrl = getConfig(Wechat.WECHAT_PAY_PAY_UNIFIED);
            String responseXml = HttpUtil.post(requestXml, requestUrl);
            logger.info("WechatPayServiceImpl.webPay.responseXml -> {}", responseXml);

            response.setResult(Jackson.fromObject(XMLUtil.xmlToMap(responseXml), WechatWebPayResponseDTO.class));
        } catch (Exception e) {
            logger.error("WechatPayServiceImpl.webPay.Exception", e);
            response.setResultCodeAndMessage(ResponseEnum.WECHAT_PAY_EXCEPTION.getCode(), ResponseEnum.WECHAT_PAY_EXCEPTION.getMessage());
        }

        return response;
    }

}
